import { ElMessage } from 'element-plus'

/**
 * 获取字典数据
 * @param type 字典类型
 * @returns 字典数据数组
 */


export const getDictionary = async (type: string): Promise<any[]> => {
  try {
    const res = await "/api/constant/list".get({ type });

const data =ref([])
    res.data.map(item=>{
      data.value.push({
        label: item.text,
        value: item.text
      })
    })


    return data.value || [];
  } catch (error) {
    console.error(`获取字典 ${type} 失败:`, error);
    ElMessage.error(`获取字典数据失败: ${type}`);
    return [];
  }
}

/**
 * 批量获取多个字典
 * @param types 字典类型数组
 * @returns 包含所有字典的对象
 */
export const getDictionaries = async (types: string[]): Promise<Record<string, any[]>> => {
  try {
    const results: Record<string, any[]> = {};

    // 使用 Promise.all 并行获取所有字典
    const promises = types.map(async (type) => {
      const data = await getDictionary(type);
      results[type] = data;
    });

    await Promise.all(promises);
    return results;
  } catch (error) {
    console.error('批量获取字典失败:', error);
    return {};
  }
}
